//index.js
//获取应用实例
const app = getApp();
const api = require('../../utils/net.handler.js');
var sliderWidth = 34;

Page({
  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    tabs: ["公告", "话题", "展览", "馆藏"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    titleFixed: '',
    toView: 'notice',
    // scrollviewheight:0
  },

  tabClick: function(e) {
    var toptop;
    var titleFixed;
    if (e.currentTarget.id === "0") {
      toptop = 'notice'
      titleFixed = ''
    }
    if (e.currentTarget.id === "1") {
      toptop = 'topic'
      titleFixed = "fixed"
    }
    if (e.currentTarget.id === "2") {
      toptop = 'exhibition'
      titleFixed = "fixed"
    }
    if (e.currentTarget.id === "3") {
      toptop = 'collection'
      titleFixed = "fixed"
    }
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id,
      toView: toptop,
      titleFixed: titleFixed
    });
  },

  scroll: function(e) {
    // console.log(e.detail.scrollTop)
    let offsetTop = 300 - e.detail.scrollTop;
    if (offsetTop < 0) {
      this.setData({
        titleFixed: "fixed",
      })
    } else {
      this.setData({
        titleFixed: "",
      })
    }
    var index = 0; // 这里的高度设置需要注意。
    if (e.detail.scrollTop < 298) {
      index = 0;
    } else if (e.detail.scrollTop > 297 && e.detail.scrollTop < 700) {
      index = 1;
    } else if (e.detail.scrollTop > 700 && e.detail.scrollTop < 900) {
      index = 2;
    } else if (e.detail.scrollTop > 900) {
      index = 3;
    }
    if (this.data.activeIndex !== index) {
      this.setData({
        sliderOffset: app.globalData.screenWidth / this.data.tabs.length * index,
        activeIndex: index
      });
    }
  },

  onLoad: function() {
    this.setData({
      sliderLeft: (app.globalData.screenWidth / this.data.tabs.length - sliderWidth) / 2,
      sliderOffset: app.globalData.screenWidth / this.data.tabs.length * this.data.activeIndex,
      // scrollviewheight: app.globalData.screenHeight  // 在这里设置好scrollview的固定高度为屏幕高度
    });
    this.summaryInfo();
    this.museumbrief();
    this.basicshow();
  },

  summaryInfo() {//博物馆简介
    var that = this;
    wx.request({
      url: app.globalData.globalUrl + "summary_info/index",
      data: {
        p: 'wx',
        type: '1',
      },
      method: 'GET',
      header: {
        'Accept': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.status == 1) {
          that.setData({
            summaryInfos: res.data.data
          });
          var info = WxParse.wxParse('infos', 'html', res.data.data.content, that);
        } else {
          wx.showModal({
            title: '网络繁忙，请稍后再试',
            showCancel: false,
          });
        }
      }
    })
  },
  museumbrief() {//场馆简介
    var that = this;
    wx.request({
      url: app.globalData.globalUrl + "museumbrief/list",
      data: {
        p: 'wx',
        take_num: '3',
      },
      method: 'GET',
      header: {
        'Accept': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.status == 1) {
          that.setData({
            museumbrief: res.data.data
          });

        } else {
          wx.showModal({
            title: '网络繁忙，请稍后再试',
            showCancel: false,
          });
        }
      }
    })
  },
  basicshow() {//基本陈列
    var that = this;
    wx.request({
      url: app.globalData.globalUrl + "basicshow/list",
      data: {
        p: 'wx',
        take_num: '3',
      },
      method: 'GET',
      header: {
        'Accept': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        wx.hideLoading();
        if (res.data.status == 1) {
          that.setData({
            basicshow: res.data.data
          });

        } else {
          wx.showModal({
            title: '网络繁忙，请稍后再试',
            showCancel: false,
          });
        }
      }
    })
  },
})